package com.corpgovernment.common.utils;

import cn.hutool.core.date.Season;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.type.ArrayType;
import com.fasterxml.jackson.databind.type.MapType;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * JacksonUtil
 * @deprecated {@link com.ctrip.corp.obt.generic.utils.JsonUtils}
 * @author jhxue
 * @create 2022-05-28-20:01
 */
@Deprecated
@Slf4j
public class JacksonUtil {

	private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

	static {
		// 忽略不存在的字段
		OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		// 忽略大小写敏感
		OBJECT_MAPPER.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
		// 枚举使用默认值处理
		OBJECT_MAPPER.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE, true);
		//允许出现特殊字符/转译字符
		OBJECT_MAPPER.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
		//允许出现单引号
		OBJECT_MAPPER.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
		//支持NULL和空字符串
		OBJECT_MAPPER.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);

	}

	/**
	 * 将对象序列号成json字符串
	 *
	 * @param obj
	 * @return
	 */
	public static String writeValue(Object obj) {
		String jsonStr = "";
		try {
			jsonStr = OBJECT_MAPPER.writeValueAsString(obj);
		} catch (Exception e) {
			log.info("将对象序列号成json字符串发生异常：{}", e);
		}
		return jsonStr;
	}

	/**
	 * 将json字符串反序列号成对象
	 *
	 * @param <T>
	 * @param json
	 * @param clazz
	 * @return
	 */
	public static <T> T readValue(String json, Class<T> clazz) {
		try {
			return OBJECT_MAPPER.readValue(json, clazz);
		} catch (Exception e) {
			log.info("将json字符串反序列号成对象异常, value:{}, clazz:{}, exception:{}", json, clazz ,e);
		}
		return null;
	}

	/**
	 * 将json字符串反序列号成数组对象
	 *
	 * @param <T>
	 * @param json
	 * @param javaType
	 * @return
	 */
	public static <T> List<T> readValue(String json, JavaType javaType) {
		try {
			return OBJECT_MAPPER.readValue(json, javaType);
		} catch (Exception e) {
			log.info("将json字符串反序列号成数组对象异常, value:{}, clazz:{}, exception:{}", json, javaType, e);
		}
		return null;
	}

	/**
	 * 反序列化
	 *
	 * @param obj
	 * @param clazz
	 * @param <T>
	 * @return
	 */
	public static <T> T readObjectValue(Object obj, Class<T> clazz) {
		String json = writeValue(obj);
		return readValue(json, clazz);
	}

	/**
	 * 反序列化(先转String)
	 *
	 * @param obj
	 * @param clazz
	 * @param <T>
	 * @return
	 */
	public static <T> List<T> readListValue(Object obj, Class<T> clazz) {
		String json = writeValue(obj);
		JavaType listAddressType = OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, clazz);
		return readValue(json, listAddressType);
	}

	/**
	 * 反序列化（不转String）
	 *
	 * @param json
	 * @param clazz
	 * @param <T>
	 * @return
	 */
	public static <T> List<T> readListValue(String json, Class<T> clazz) {
		JavaType listAddressType = OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, clazz);
		return readValue(json, listAddressType);
	}


	public static <T> T readObjectList (Object obj, Class<T> clazz) {
		String json = writeValue(obj);
		JavaType javaType = OBJECT_MAPPER.getTypeFactory().constructParametricType(List.class, clazz);
		try {
			return OBJECT_MAPPER.readValue(json, javaType);
		} catch (JsonProcessingException e) {
			log.error("List转换异常:{}", e);
		}
		return null;
	}

	/**
	 * 对象拷贝
	 * @param value
	 * @param tClass
	 * @param <T>
	 * @return
	 */
	public static <T> T copyValue(Object value, Class<T> tClass){
		return OBJECT_MAPPER.convertValue(value, tClass);
	}

	/**
	 * 获取Json树结构
	 * @param json
	 * @return
	 */
	public static JsonNode getJsonNode(String json){
		try {
			return OBJECT_MAPPER.readTree(json);
		} catch (JsonProcessingException e) {
			log.info("获取Json树结构异常：{}", e);
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @param json
	 * @param clazz
	 * @param <T>
	 * @return
	 */
	public static <T> Map readValueMap(String json, Class<T> clazz, Integer type) {
		try {
			if (1 == type){
				//Map<String, List<clazz>>
				JavaType arrJavaType = OBJECT_MAPPER.getTypeFactory().constructCollectionType(ArrayList.class, clazz);
				JavaType javaType = OBJECT_MAPPER.getTypeFactory().constructType(Object.class);
				MapType mapType = OBJECT_MAPPER.getTypeFactory().constructMapType(HashMap.class, javaType, arrJavaType);
				return OBJECT_MAPPER.readValue(json, mapType);
			} else if (2 == type){
				//Map<String, clazz>
				MapType mapType = OBJECT_MAPPER.getTypeFactory().constructMapType(HashMap.class, Object.class, clazz);
				return OBJECT_MAPPER.readValue(json, mapType);
			}
		} catch (Exception e) {
			log.info("获取转换Map异常, value:{}, clazz:{}, exception{}", json, clazz, e);
			e.printStackTrace();
		}
		return null;
	}
}
